#include "FileSelect.h"
#include "ui_FileSelect.h"

FileSelect::FileSelect(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::FileSelect)
{
    m_ui->setupUi(this);
//	Initialize();
}

FileSelect::~FileSelect()
{
    delete m_ui;
}

void FileSelect::Initialize(bool isFileSelect)
{
	_model.setReadOnly(true);
	if (!isFileSelect)
	{
		setWindowTitle("Choose Directory");
		_model.setFilter(QDir::Dirs);
	}
	else
		setWindowTitle("Choose File");
	_model.setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
	m_ui->treeView->setModel(&_model);
	_modelIndex = _model.index(".");
	m_ui->treeView->expand(_modelIndex);
	m_ui->treeView->setCurrentIndex(_modelIndex);
	_isFileSelect = isFileSelect;
}

void FileSelect::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void FileSelect::on_button_ok_clicked()
{
	QModelIndex index = m_ui->treeView->currentIndex();
	if (_isFileSelect)
	{
		if (index.isValid() && !_model.isDir(index))
		{
			emit sigFileSeleted(_model.filePath(index), _model.fileName(index));
			hide();
		}
	}
	else
	{
		if (index.isValid() && _model.isDir(index))
		{
			emit sigDirectorySelected(_model.filePath(index));
			hide();
		}
	}
}

void FileSelect::on_button_cancel_clicked()
{
	hide();
}
